﻿using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class EditCategoryPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IEditCategoryView _view;

        public EditCategoryPresenter(IEditCategoryView view)
            : this(view, new RepositoryFactory())
        {
        }

        public EditCategoryPresenter(IEditCategoryView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditCategory)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            _view.SetParentCategories(_administrator.CurrentSite.GetRootCategories());
            if (_view.CategoryID == -1)
            {
                _view.SelectedDefaultSortKey = DefaultCategorySortKey.None;
            }
            else
            {
                Category category = CategoryRepository.GetCategory(_view.CategoryID);
                _view.Name = category.Name;
                _view.FolderName = category.FolderName;
                _view.SelectedDefaultSortKey = category.DefaultSortKey;
                _view.SelectedParentCategoryID = category.Parent != null ? category.Parent.ID : -1;
            }
        }
        
        public void Save()
        {
            ValidateBeforeSave();
            if (!IsValid)
            {
                return;
            }
            Category category;
            if (_view.CategoryID == -1)
            {
                category = new Category { Site = _administrator.CurrentSite };
                category.Site.Categories.Add(category);
            }
            else
            {
                category = CategoryRepository.GetCategory(_view.CategoryID);
            }
            category.Name = _view.Name;
            category.FolderName = _view.FolderName;
            category.DefaultSortKey = _view.SelectedDefaultSortKey;
            category.Parent = PageRepository.GetCategory(_view.SelectedParentCategoryID);
            CategoryRepository.SaveCategory(category);
        }

        private void ValidateBeforeSave()
        {
            ValidateThatNameExistAndIsNotDuplicated();
            //ValidateThatFolderNameExistAndIsNotDuplicated();
            ValidateSaveInSelfError();
            ValidateSaveInChildError();
        }

        private void ValidateSaveInChildError()
        {
            if (_view.CategoryID > 0)
            {
                Category editedCategory = PageRepository.GetCategory(_view.CategoryID);
                Category selectedParentCategory = PageRepository.GetCategory(_view.SelectedParentCategoryID);
                if (editedCategory.IsParentTo(selectedParentCategory))
                {
                    AddValidationError(4, GetText(_view, "SaveInChildError"));
                }
            }
        }

        private void ValidateSaveInSelfError()
        {
            if (_view.CategoryID > 0 && _view.SelectedParentCategoryID == _view.CategoryID)
            {
                AddValidationError(3, GetText(_view, "SaveInSelfError"));
            }
        }

        private void ValidateThatNameExistAndIsNotDuplicated()
        {
            if (_view.Name.Trim() == "")
            {
                AddValidationError(GetText(_view, "NoNameError"));
            }
            else if (CategoryRepository.CategoryExists(_view.Name, _view.CategoryID, _administrator.CurrentSite.ID))
            {
                AddValidationError(GetText(_view, "CategoryNameExistsError"));
            }
        }

        private void ValidateThatFolderNameExistAndIsNotDuplicated()
        {
            if (_view.FolderName.Trim() == "")
            {
                AddValidationError(GetText(_view, "NoFolderNameError"));
            }
            else if (CategoryRepository.CategoryFolderNameExists(_view.FolderName, _view.CategoryID, _administrator.CurrentSite.ID))
            {
                AddValidationError(GetText(_view, "CategoryFolderNameExistsError"));
            }
        }
    }
}